package com.pattern.bulider;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 1、定义具体产品
 */
@Data
@NoArgsConstructor
class Computer {
    private String cpu;
    private String screen;
    private String memory;
    private String mainboard;
}
/**
 * 2、抽象的建造者
 */
abstract class Builder{
    abstract void buildCpu();
    abstract void buildScreen();
    abstract void buildMemory();
    abstract void buildMainboard();
    abstract Computer create();
}

/**
 * 3、具体的建造者
 */
//戴尔的建造者
class DellBuilder extends Builder{
    Computer computer = new Computer();

    @Override
    void buildCpu() {
        computer.setCpu("Dell CPU");
    }

    @Override
    void buildScreen() {
        computer.setScreen("Dell Screen");
    }

    @Override
    void buildMemory() {
        computer.setMemory("Dell memory");
    }

    @Override
    void buildMainboard() {
        computer.setMainboard("SanXing");
    }

    @Override
    Computer create() {
        return computer;
    }
}
//惠普的建造者
class HpBuilder extends Builder{
    Computer computer = new Computer();

    @Override
    void buildCpu() {
        computer.setCpu("Intel CPU");
    }

    @Override
    void buildScreen() {
        computer.setScreen("HP Screen");
    }

    @Override
    void buildMemory() {
        computer.setMemory("HP memory");
    }

    @Override
    void buildMainboard() {
        computer.setMainboard("HP");
    }

    @Override
    Computer create() {
        return computer;
    }
}

/**
 * 4、指挥者
 */
class Director {
    private Builder mBuilder = null;
    public Director(Builder builder) {
        mBuilder = builder;
    }
    public Computer construct() {
        mBuilder.buildCpu();
        mBuilder.buildScreen();
        mBuilder.buildMemory();
        mBuilder.buildMainboard();
        return mBuilder.create();
    }
}

/**
 * 建造者模式
 * @author LYao15
 */
public class BuilderTest {
    public static void main(String[] args) {
        System.out.println("惠普电脑：" + showBike(new HpBuilder()));
        System.out.println("戴尔电脑：" + showBike(new DellBuilder()));
    }
    private static Computer showBike(Builder builder) {
        Director director = new Director(builder);
        return director.construct();
    }

}
